package com.example.demo.web.controller;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import com.example.demo.api.AipImageClient;
import com.example.demo.bean.CallLog;
import com.example.demo.service.ICallLogService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.baidu.aip.imageclassify.AipImageClassify;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @program:
 * @description: 智能识别接口
 * @author: jacky
 * @create: 2020-10-11 15:51
 **/
@Api(description = "图像识别相关接口")
@Validated
@RestController
@RequestMapping("/recognition")
public class RecognitionController {
	@Autowired
	private ICallLogService callLogService;
	@ApiOperation(value = "植物识别")
	@PostMapping("image")
	public void image( @RequestParam("file") MultipartFile file,HttpServletResponse resp) throws Exception {
		// 获取图像识别客户端
		AipImageClassify client = AipImageClient.getInstance();
		//调用sdk完成识别功能
		JSONObject res = client.advancedGeneral(file.getBytes(), new HashMap<>());
		// 手动返回json
//		定义相应报文相应头信息
		resp.setHeader("Content-Type", "application/json;charset=UTF-8");
		CallLog log=new CallLog();
		log.setCallTime(new Date().getTime());
		log.setComment(res.toString());
		callLogService.save(log);
		//		将识别结果写入到相应体
		resp.getWriter().write(res.toString());

	}
}
